/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package stage;

import model.StageMapped;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static model.StageMapped.StageMappedComparators.*;

/**
 * @author Lionel
 */
@WebServlet(name = "StageManagerServlet", urlPatterns = {"/StageManagerServlet"})
public class StageManagerServlet extends HttpServlet {

    public static void firstTenSporttraining(HttpServletRequest request, List<StageMapped> listStage) {
        request.setAttribute("start", 0);
        request.setAttribute("end", listStage.size() > 10 ? 10 : listStage.size());

    }

    public static void next(HttpServletRequest request, List<StageMapped> listStage) {
        if (request.getParameter("start") != null && request.getParameter("end") != null) {
            int end = Integer.parseInt(request.getParameter("end"));
            request.setAttribute("start", end);
            request.setAttribute("end", listStage.size() != end && listStage.size() > end + 10 ? end + 10 : listStage.size());
        }

    }

    public void StageNouveaute(HttpServletRequest request, List<StageMapped> listStage, List<StageMapped> tempList) {
        if (request.getParameter("nouveaute").equals("0")) for (StageMapped stage : tempList) {
            int LastStageID = tempList.get(tempList.size() - 1).getSportrainingID();
            if (stage.getSportrainingID() >= (LastStageID - 5)) listStage.add(stage);
            firstTenSporttraining(request, listStage);
        }
        next(request, listStage);
        Collections.sort(listStage, Collections.reverseOrder(StageID));
        request.setAttribute("nouveaute", "nouveaute");
        request.setAttribute("critere", "<strong>Nouveautées</strong>");
    }

    public void StagePromo(HttpServletRequest request, List<StageMapped> listStage, List<StageMapped> tempList) {
        if (request.getParameter("promo").equals("0")) {
            for (StageMapped stage : tempList)
                if (stage.getPrice() <= 100.0 && stage.getDuration() == 2 && stage.getRegistrationAvailable() <= 5)
                    listStage.add(stage);
            firstTenSporttraining(request, listStage);
            request.setAttribute("critere", "<strong>Promotions: Stage de 2 jours de moins de 100€  avec moins de 5 place disponible</strong>");

        }
        if (request.getParameter("promo").equals("1")) {
            for (StageMapped stage : tempList) {
                if (stage.getRegistrationAvailable() == 1)
                    listStage.add(stage);
            }
            firstTenSporttraining(request, listStage);
            request.setAttribute("critere", "<strong>Last Minute: Stage où il ne reste qu'une place disponible </strong>");

        }
        if (request.getParameter("promo").equals("<5")) {
            for (StageMapped stage : tempList)
                if (stage.getRegistrationAvailable() < 5 && stage.getRegistrationAvailable() != 0) {
                    listStage.add(stage);
                }
            firstTenSporttraining(request, listStage);
            request.setAttribute("critere", "<strong>Last Minute: Stage où il ne reste plus moins 5 places disponible ! </strong>");

        }
        if (request.getParameter("promo").equals("<10")) {
            for (StageMapped stage : tempList) {
                if (stage.getRegistrationAvailable() < 10 && stage.getRegistrationAvailable() != 0) {
                    listStage.add(stage);
                }
            }
            firstTenSporttraining(request, listStage);
            request.setAttribute("critere", "<strong>Last Minute: Stage où il ne reste plus moins 10 places disponible ! </strong>");

        }
        if (request.getParameter("promo").equals("1-<50")) {
            for (StageMapped stage : tempList) {
                if (stage.getDuration() == 1 && stage.getPrice() < 50.0 && stage.getRegistrationAvailable() != 0) {
                    listStage.add(stage);
                }
            }
            firstTenSporttraining(request, listStage);
            request.setAttribute("critere", "<strong>Last Minute: Stage de 1 jour à moins de 50€ ! </strong>");

        }
        if (request.getParameter("promo").equals("1-<100")) {
            for (StageMapped stage : tempList) {
                if (stage.getDuration() == 1 && stage.getPrice() < 100.0 && stage.getRegistrationAvailable() != 0) {
                    listStage.add(stage);
                }
            }
            firstTenSporttraining(request, listStage);
            request.setAttribute("critere", "<strong>Last Minute: Stage de 1 jour à moins de 100€ ! </strong>");

        }
        if (request.getParameter("promo").equals("2-<50")) {
            for (StageMapped stage : tempList) {
                if (stage.getDuration() == 2 && stage.getPrice() < 50.0 && stage.getRegistrationAvailable() != 0) {
                    listStage.add(stage);
                }
            }
            firstTenSporttraining(request, listStage);
            request.setAttribute("critere", "<strong>Last Minute: Stage de 2 jours à moins de 50€ ! </strong>");

        }
        if (request.getParameter("promo").equals("2-<100")) {
            for (StageMapped stage : tempList) {
                if (stage.getDuration() == 2 && stage.getPrice() < 100.0 && stage.getRegistrationAvailable() != 0) {
                    listStage.add(stage);
                }
            }
            firstTenSporttraining(request, listStage);
            request.setAttribute("critere", "<strong>Last Minute: Stage de 2 jours à moins de 100€ ! </strong>");

        }


        request.setAttribute("promo", "promo");
        next(request, listStage);
        Collections.sort(listStage, Price);
    }

    public void StageByLevel(HttpServletRequest request, List<StageMapped> listStage, List<StageMapped> tempList) {
        if (request.getParameter("level").equals("1")) {
            for (StageMapped stage : tempList) {
                if (stage.getLevel() == 1) {
                    listStage.add(stage);
                }
            }
            firstTenSporttraining(request, listStage);
            request.setAttribute("critere", "<strong>Liste des stages de niveau 1</strong>");

        }
        if (request.getParameter("level").equals("2")) {
            for (StageMapped stage : tempList) {
                if (stage.getLevel() == 2) {
                    listStage.add(stage);
                }
            }
            firstTenSporttraining(request, listStage);
            request.setAttribute("critere", "<strong>Liste des stages de niveau 2</strong>");

        }
        if (request.getParameter("level").equals("3")) {
            for (StageMapped stage : tempList) {
                if (stage.getLevel() == 3) {
                    listStage.add(stage);
                }
            }
            firstTenSporttraining(request, listStage);
            request.setAttribute("critere", "<strong>Liste des stages de niveau 3</strong>");

        }
        if (request.getParameter("level").equals("4")) {
            for (StageMapped stage : tempList) {
                if (stage.getLevel() == 4) {
                    listStage.add(stage);
                }
            }
            firstTenSporttraining(request, listStage);
            request.setAttribute("critere", "<strong>Liste des stages de niveau 4</strong>");

        }
        if (request.getParameter("level").equals("5")) {
            for (StageMapped stage : tempList) {
                if (stage.getLevel() == 5) {
                    listStage.add(stage);
                }
            }
            firstTenSporttraining(request, listStage);
            request.setAttribute("critere", "<strong>Liste des stages de niveau 5</strong>");
        }
        next(request, listStage);
        Collections.sort(listStage, Price);
    }

    public void StageByAge(HttpServletRequest request, List<StageMapped> listStage, List<StageMapped> tempList) {
        if (request.getParameter("age").equals("-10")) {
            for (StageMapped stage : tempList) {
                if (stage.getMinAge() < 10) {
                    listStage.add(stage);
                }
            }
            firstTenSporttraining(request, listStage);
            request.setAttribute("critere", "<strong>Liste des stages pour les moins de 10 ans</strong>");

        }
        if (request.getParameter("age").equals("10")) {
            for (StageMapped stage : tempList) {
                if (stage.getMinAge() == 10) {
                    listStage.add(stage);
                }
            }
            firstTenSporttraining(request, listStage);
            request.setAttribute("critere", "<strong>Liste des stages à partir de 10 ans</strong>");

        }
        if (request.getParameter("age").equals("10-12")) {
            for (StageMapped stage : tempList) {
                if (stage.getMinAge() >= 10 && stage.getMinAge() < 12) {
                    listStage.add(stage);
                }
            }
            firstTenSporttraining(request, listStage);
            request.setAttribute("critere", "<strong>Liste des stages pour les 10 à 12ans </strong>");

        }
        if (request.getParameter("age").equals("12")) {
            for (StageMapped stage : tempList) {
                if (stage.getMinAge() == 12) {
                    listStage.add(stage);
                }
            }
            firstTenSporttraining(request, listStage);
            request.setAttribute("critere", "<strong>Liste des stages à partir de 12 ans</strong>");

        }
        if (request.getParameter("age").equals("12-16")) {
            for (StageMapped stage : tempList) {
                if (stage.getMinAge() >= 12 && stage.getMinAge() < 16) {
                    listStage.add(stage);
                }
            }
            firstTenSporttraining(request, listStage);
            request.setAttribute("critere", "<strong>Liste des stages pour les 12 à 16 ans</strong>");

        }
        if (request.getParameter("age").equals("16")) {
            for (StageMapped stage : tempList) {
                if (stage.getMinAge() == 16) {
                    listStage.add(stage);
                }
            }
            firstTenSporttraining(request, listStage);
            request.setAttribute("critere", "<strong>Liste des stages à partir de 16 ans</strong>");

        }
        if (request.getParameter("age").equals("16-18")) {
            for (StageMapped stage : tempList) {
                if (stage.getMinAge() >= 16 && stage.getMinAge() < 18) {
                    listStage.add(stage);
                }
            }
            firstTenSporttraining(request, listStage);
            request.setAttribute("critere", "<strong>Liste des stages pour les 16 à 18 ans</strong>");

        }
        if (request.getParameter("age").equals("18")) {
            for (StageMapped stage : tempList) {
                if (stage.getMinAge() == 18) {
                    listStage.add(stage);
                }
            }
            firstTenSporttraining(request, listStage);
            request.setAttribute("critere", "<strong>Liste des stages pour les adultes uniquement</strong>");

        }
        next(request, listStage);
        Collections.sort(listStage, Age);
    }

    public void StageByDays(HttpServletRequest request, List<StageMapped> listStage, List<StageMapped> tempList) {

        if (request.getParameter("jour").equals("1")) {
            for (StageMapped stage : tempList) {
                if (stage.getDuration() == 1) {
                    listStage.add(stage);
                }
            }
            firstTenSporttraining(request, listStage);
            request.setAttribute("critere", "<strong>Liste des stages d'une durée de 1 jour</strong>");
        }
        if (request.getParameter("jour").equals("2")) {
            for (StageMapped stage : tempList) {
                if (stage.getDuration() == 2) {
                    listStage.add(stage);
                }
            }
            firstTenSporttraining(request, listStage);
            request.setAttribute("critere", "<strong>Liste des stages d'une durée de 2 jours</strong>");

        }
        next(request, listStage);

        Collections.sort(listStage, Price);

    }

    public void StageByPrice(HttpServletRequest request, List<StageMapped> listStage, List<StageMapped> tempList) {
        if (request.getParameter("price").equals("-25")) {

            for (StageMapped stage : tempList) {
                if (stage.getPrice() <= 25.0d) {
                    listStage.add(stage);
                }
            }
            firstTenSporttraining(request, listStage);
            request.setAttribute("critere", "<strong>Liste des stages  de moins de 25€</strong>");
        }
        if (request.getParameter("price").equals("25-50")) {

            for (StageMapped stage : tempList) {
                if (stage.getPrice() > 25.0d && stage.getPrice() <= 50.0d) {
                    listStage.add(stage);
                }
            }
            firstTenSporttraining(request, listStage);
            request.setAttribute("critere", "<strong>Liste des stages  entre 25 et 50€</strong>");

        }
        if (request.getParameter("price").equals("50-100")) {

            for (StageMapped stage : tempList) {
                if (stage.getPrice() > 50.0d && stage.getPrice() <= 100.0d) {
                    listStage.add(stage);
                }
            }
            firstTenSporttraining(request, listStage);
            request.setAttribute("critere", "<strong>Liste des stages  entre 50 et 100€</strong>");
        }
        if (request.getParameter("price").equals("100-150")) {

            for (StageMapped stage : tempList) {
                if (stage.getPrice() > 100.0d && stage.getPrice() <= 150.0d) {
                    listStage.add(stage);
                }
            }
            firstTenSporttraining(request, listStage);
            request.setAttribute("critere", "<strong>Liste des stages  entre 100 et 150€</strong>");
        }
        if (request.getParameter("price").equals(">150")) {

            for (StageMapped stage : tempList) {
                if (stage.getPrice() > 150.0d) {
                    listStage.add(stage);
                }
            }
            firstTenSporttraining(request, listStage);
            request.setAttribute("critere", "<strong>Liste des stages de plus de 150 €</strong>");
        }
        next(request, listStage);
        Collections.sort(listStage, Price);
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request  servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException      if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        List<StageMapped> listStage = new ArrayList<>();
        List<StageMapped> tempList;

        try {
            tempList = StageManager.GetStageList();
            if (request.getParameter("id") != null) {
                if (request.getParameter("id").equals("0"))
                    listStage = tempList;
                request.setAttribute("critere", "<strong>Liste de tous les stages</strong>");
                request.setAttribute("start", 0);
                request.setAttribute("end", listStage.size() > 10 ? 10 : listStage.size());
                next(request, listStage);
            }
            if (request.getParameter("level") != null)
                StageByLevel(request, listStage, tempList);
            if (request.getParameter("jour") != null)
                StageByDays(request, listStage, tempList);
            if (request.getParameter("price") != null)
                StageByPrice(request, listStage, tempList);
            if (request.getParameter("age") != null)
                StageByAge(request, listStage, tempList);

            if (request.getParameter("promo") != null) StagePromo(request, listStage, tempList);
            if (request.getParameter("nouveaute") != null)
                StageNouveaute(request, listStage, tempList);
            request.setAttribute("listeStages", listStage);
            RequestDispatcher view = request.getRequestDispatcher("Site/Stage/rechercherStage.jsp");
            view.forward(request, response);

        } catch (Exception e) {
            request.setAttribute("Error", "Une erreur est survenue("+e.getMessage() +")<br><strong>Cela peut provenir:</strong> <br>- D'une erreur technique (Erreur dans le code )<br> -Le webservice est injoignable<br>Veuillez réessayer plus tard !");
            RequestDispatcher view = request.getRequestDispatcher("Site/error/error.jsp");
            view.forward(request, response);
        }


    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request  servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException      if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
